package service.impl.security;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import service.impl.BaseDataServiceImpl;
import service.interf.security.IMenu;

import common.impl.exception.ServiceException;

import dao.interf.IGroupDAO;
import dao.interf.IGroupMenuDAO;
import dao.interf.IMenuDAO;
import dao.pojo.Group;
import dao.pojo.GroupMenu;
import dao.pojo.Menu;

/**
 * Title:MenuImpl Description:提供菜单相关接口
 * 
 * @author yangl
 * @date 2012-11-30
 * @version 1.0
 */
public class MenuImpl extends BaseDataServiceImpl implements IMenu {
	private IMenuDAO menuDAO;
	private IGroupDAO groupDAO;
	private IGroupMenuDAO groupMenuDAO;

	public IGroupMenuDAO getGroupMenuDAO() {
		return groupMenuDAO;
	}

	public void setGroupMenuDAO(IGroupMenuDAO groupMenuDAO) {
		this.groupMenuDAO = groupMenuDAO;
	}

	public IGroupDAO getGroupDAO() {
		return groupDAO;
	}

	public void setGroupDAO(IGroupDAO groupDAO) {
		this.groupDAO = groupDAO;
	}

	public IMenuDAO getMenuDAO() {
		return menuDAO;
	}

	public void setMenuDAO(IMenuDAO menuDAO) {
		this.menuDAO = menuDAO;
	}

	// 添加menu
	public String addMenu(Menu model) {
		Menu menu = menuDAO.save(model);
		return menu.getMenuId().toString();
	}

	// 查找所有的menu
	// 修改menu
	public String editMenu(Menu model) throws Exception {
		// 查找menu
		Menu menu = menuDAO.findById(model.getMenuId());
		if (menu == null) {
			throw new ServiceException("ERR-0010");
		}
		// 更新menu
		menuDAO.update(model);
		return menu.getMenuId().toString();
	}

	// 删除menu
	public String delMenuByIds(String ids) throws Exception {
		String[] idS = ids.split(",");
		for (int i = 0; i < idS.length; i++) {
			// 查找该菜单是否存在
			Menu menu = menuDAO.findById(new Integer(idS[i]));
			if (menu == null) {
				throw new ServiceException("ERR-0010");
			}
			// 删除menu
			menuDAO.delete(menu);
		}
		return "1";
	}

	/**
	 * 根据登录的用户获取组菜单
	 */
	public Map<String, List<Menu>> getGroupMenusByUserId(List<Group> gL) {
		Map<String, List<Menu>> menuList = new HashMap<String, List<Menu>>();
		if (null == gL || gL.isEmpty())
			return menuList;
		List<Integer> idL = new ArrayList<Integer>();
		for (Group g : gL)
			idL.add(g.getGroupId());
		List<GroupMenu> listGroupMenu = groupMenuDAO.findByPropertiesRule(
				new String[] { "group.groupId" }, new Object[] { idL },
				new String[] { "in" }, null, 0, 0);
		if (null == listGroupMenu || listGroupMenu.isEmpty())
			return menuList;
		// 存放菜单
		for (GroupMenu gm : listGroupMenu) {
			// 是父菜单
			Menu menu = gm.getMenu();
			if (null == menu)
				continue;
			String url = menu.getUrl();
			if (!StringUtils.isEmpty(url)) {
				ArrayList<Menu> map;
				String title;
				if (menu.getMenu() != null) {
					title = menu.getMenu().getTitle();
				} else {
					title = "";
				}
				map = (ArrayList<Menu>) menuList.get(title);
				if (null == map)
					map = new ArrayList<Menu>();
				map.add(menu);
				menuList.put(title, map);
			}
		}
		return menuList;
	}
}
